<template lang="pug">
.page
  .CustomerSalaryDetail
    .pl-20.pr-20.bg-white
      Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="customerSalaryTable" @on-selection-change="handleOnSelectChange")
        .clearfix.font-16(slot="header")
          .table-title-lefttxt.pull-left
            |合计
            span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
            |条

        template(slot-scope="{ row, index }" slot="action") ￥{{row.realSalaryTotal}}

        .clearfix.pl-20.pr-20(slot="footer")
          Page(
            :current="tableComone.pageIndex"
            :total="tableComone.totalRows"
            @on-change="onChangePage"
            @on-page-size-change="onpageSizeChange"
            show-sizer
            show-total
            placement="top"
            prev-text="上一页"
            next-text="下一页"
            :page-size-opts="[10, 20, 30, 50, 100]"
          )
</template>

<script>
import { SysInitCustomerSalaryDetailApi } from '@/api/SysInitCustomerSalaryDetail.api.js'
import { EmployeeSalaryApi } from '@/api/EmployeeSalary.api.js'
import { Common } from '@/utils/common.js'
import { salaryDetailThead } from './CStableHead.js'
import _cloneDeep from 'clone-deep'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')
export default {
  name: 'SysInitCustomerSalaryDetail',

  components: {
  },

  data () {
    return {
      salaryId: '',
      salaryMonth: '',
      thead: _cloneDeep(salaryDetailThead),
      tableData: [],
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10 // 当前页面展示条数
      },
      selectedEmployeeId: [], // 勾选的id集合,array格式
      selectedEmployeeArr: [], // 勾选的id, name数组集合
      formData: {
        name: '',
        month: ''
      },
      downLoadUrl: ''
    }
  },

  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    }
  },

  watch: {
  },

  mounted () {
    this.salaryId = this.$route.query.id
    this.year = this.$route.query.year
    this.query()
  },

  methods: {
    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },

    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        SalaryGUID: this.salaryId,
        year: this.year,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await SysInitCustomerSalaryDetailApi.getList(params)
      console.log('客户薪资res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
      this.loading = false
    },

    handleExportData (ref, filename) {
      Common.handleExportData(this.$refs.customerSalaryTable, '客户薪资明细')
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedEmployeeArr = value.map((item) => {
        return { id: item.id, name: item.name }
      })
      this.selectedEmployeeId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedEmployeeArr)
      console.log('列表勾选数组2', this.selectedEmployeeId)
    },


    async handleExport () {
      var params = {
        ids: this.selectedEmployeeId.join(','),
        salaryGUID: this.$route.query.id,
        salaryMonth: this.$route.query.month
      }
      let res = await EmployeeSalaryApi.export(params)
      console.log('客户薪资导出res: ', res)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.data)
        window.open(baseUrl + res.data.data, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },
  }
}
</script>

<style lang="less" scope>
</style>
